package com.brillilab.service.core.kit.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.kit.KitSpecMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.po.kit.KitSpec;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.core.kit.IKitSpecService;
import com.brillilab.service.core.system.IDictService;

import javax.annotation.Resource;

@Service
@Transactional
public class KitSpecServiceImpl implements IKitSpecService {

	@Resource
	private KitSpecMapper kitSpecMapper;
	@Resource
	private IDictService dictService;
	@Resource
	private IKitService kitService;
	
	@Override
	public KitSpec insert(KitSpec entity) {
		Assert.isTrue(entity.getSize()!=null && entity.getSize().compareTo(BigDecimal.ZERO)==1 
				&& entity.getKitSize()!=null && entity.getKitSize().compareTo(BigDecimal.ZERO)==1 
				&& entity.getMultiple()!=0 ,"规格错误");
		Assert.isTrue(entity.getMultiple()<100, "试剂支数不能大于99");
		Integer insert = kitSpecMapper.insert(entity);
		Assert.isTrue(insert == 1, "添加失败");
		return entity;
	}

	@Override
	public boolean deleteById(Long id) {
		Assert.notNull(id, "删除ID不能为空");
		KitSpec entity = new KitSpec();
		entity.setId(id);
		entity.setIsDelete(IsDeleteEnum.YES.getValue());
		Integer b = kitSpecMapper.updateById(entity);
		return b == 1 ? true : false;
	}

	@Override
	public boolean updateById(KitSpec entity) {
		Assert.isTrue(entity.getSize()!=null && entity.getSize().compareTo(BigDecimal.ZERO)==1 
				&& entity.getKitSize()!=null &&  entity.getKitSize().compareTo(BigDecimal.ZERO)==1 
				&& entity.getMultiple()!=0 ,"规格错误");
		entity.setUpdateTime(new Date());
		Integer b = kitSpecMapper.updateById(entity);
		return b == 1 ? true : false;
	}

	@Override
	public KitSpec selectById(Long id) {
		return kitSpecMapper.selectById(id);
	}

	@Override
	public List<KitSpec> getKitSpecListBySpecId(Long specId) {
		KitSpec spec = kitSpecMapper.selectById(specId);
		LambdaQueryWrapper<KitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(KitSpec::getKitSize, spec.getKitSize());
		wrapper.eq(KitSpec::getKitId, spec.getKitId());
		wrapper.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		return kitSpecMapper.selectList(wrapper);
	}

	@Override
	public List<KitSpec> selectList(Long kitId, BigDecimal kitSize) {
		LambdaQueryWrapper<KitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(KitSpec::getKitId, kitId);
		if (kitSize != null)
			wrapper.eq(KitSpec::getKitSize, kitSize);
		wrapper.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		return kitSpecMapper.selectList(wrapper);
	}
	
	@Override
	public List<KitSpec> selectListByIds(Long kitId, Long[] spectIds) {
		LambdaQueryWrapper<KitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(KitSpec::getKitId, kitId);
		wrapper.in(KitSpec::getId, spectIds);
		wrapper.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		return kitSpecMapper.selectList(wrapper);
	}

	@Override
	public List<KitSpec> selectList(Long kitId) {
		return kitSpecMapper.selectList(new LambdaQueryWrapper<KitSpec>()
				.eq(KitSpec::getKitId, kitId)
				.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue()));
	}

	@Override
	public List<KitSpec> selectList(List<Long> kitIds) {
		if(kitIds!=null && kitIds.size()>0){
			return kitSpecMapper.selectList(new LambdaQueryWrapper<KitSpec>()
					.in(KitSpec::getKitId, kitIds)
					.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue()));
		}
		return new ArrayList<>();
	}

	@Override
	public List<KitSpec> selectSpecGroup(Long kitId) {
		List<KitSpec> specList = new ArrayList<>();
		Map<BigDecimal, KitSpec> map = new HashMap<>();
		LambdaQueryWrapper<KitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(KitSpec::getKitId, kitId);
		wrapper.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		List<KitSpec> list = kitSpecMapper.selectList(wrapper);
		list.forEach(kitSpec -> {
			//kit规格分组
			KitSpec spec = map.get(kitSpec.getKitSize());
			if(spec==null){
				map.put(kitSpec.getKitSize(), kitSpec);
				specList.add(kitSpec);
			}
		});
		return specList;
	}
	
	@Override
	public Map<Long,List<KitSpec>> selectSpecGroup(List<Long> kitIds) {
		Map<Long, List<KitSpec>> groupMap = new HashMap<>();
		//1、获取所有规格
		LambdaQueryWrapper<KitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		wrapper.in(KitSpec::getKitId, kitIds);
		List<KitSpec> list = kitSpecMapper.selectList(wrapper);
		if(list!=null && list.size()>0){
			//2、根据kitId分组规格
			Map<Long, List<KitSpec>> collectMap = list.stream().collect(Collectors.groupingBy(KitSpec::getKitId));
			collectMap.keySet().stream().forEach(kitId -> {
				List<KitSpec> list2 = collectMap.get(kitId);
				if(list2!=null && list2.size()>0) {
					KitSpec kitSpec = list2.get(0);
					Map<BigDecimal, List<KitSpec>> kitSpecMap = list2.stream().collect(Collectors.groupingBy(KitSpec::getKitSize));
					groupMap.put(kitId, kitSpecMap.get(kitSpec.getKitSize()));
				}
			});
		}
		return groupMap;
	}

	@Override
	public List<KitSpec> selectSpecList(Long kitId, Long reagentId) {
		LambdaQueryWrapper<KitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(KitSpec::getKitId, kitId);
		wrapper.eq(KitSpec::getReagentId, reagentId);
		wrapper.eq(KitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		return kitSpecMapper.selectList(wrapper);
	}

	@Override
	public String getSpecName(List<KitSpec> specList, Integer type) {
		String specName = "";
		if(specList!=null &&specList.size()>0){
			Map<Integer, String> unitMap = dictService.getUnitMap();
			//组装规格信息
			if(type==0){//0-Kit规格
				int i = 1;
				Kit kit = kitService.selectById(specList.get(0).getKitId());
				String unitName = unitMap.get(kit.getUnit());
				for (KitSpec kitSpec : specList) {
					if (kit.getKitType() == 0) {// 单成分
						specName += kitSpec.getSize().stripTrailingZeros().toPlainString() + unitName;
						if (kitSpec.getMultiple() > 1) specName += "*" + kitSpec.getMultiple();
					} else {// 多成分
						specName += kitSpec.getKitSize().stripTrailingZeros().toPlainString() + unitName;
					}
					if(i<specList.size()){
						specName += ",";
					}
					i++;
				}
			}else{//1-成分规格
				int i=1;
				for (KitSpec kitSpec : specList) {
					specName += kitSpec.getSize().stripTrailingZeros().toPlainString() + unitMap.get(kitSpec.getUnit());
					if (kitSpec.getMultiple() > 1)
						specName += "*" + kitSpec.getMultiple();
					if(i<specList.size()){
						specName += ",";
					}
					i++;
				}
			}
		}
		return specName;
	}

	@Override
	public void insertBatch(List<KitSpec> specList) {
		if(specList!=null && specList.size()>0){
            Integer count = kitSpecMapper.insertBatch(specList);
            Assert.isTrue(count.equals(specList.size()), "添加失败");
        }
	}

}
